package cn.melon.view.control;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JPasswordField;

/**
 * 监听器，用于实现密码输入框提示文本的功能
 * @ClassName: PassInputTip
 * @author Hzy
 */
public class PassInputTip implements FocusListener{
	private JPasswordField passInput;
	private String tip;
	private String pass;
	public PassInputTip(JPasswordField passInput,String tip){
		this.passInput = passInput;
		this.tip = tip;
	}
	
	@Override
	public void focusGained(FocusEvent e){
		this.pass = new String(passInput.getPassword());
//		获得焦点且其中文本为提示文本时，设置字体颜色和回显符，并清空tip
		if(pass.equals(tip)){
			passInput.setEchoChar('*');
			passInput.setForeground(Color.BLACK);
			passInput.setText("");
		}
	}
	
	@Override
	public void focusLost(FocusEvent e){
		this.pass = new String(passInput.getPassword());
		if(pass.equals("")){
//			失去焦点且其中文本为空时，设置字体颜色和回显符，并设置tip
			passInput.setEchoChar('\0');
			passInput.setForeground(Color.gray);
			passInput.setText(tip);
		}
	}
}
